using System;
using Microsoft.WowAddonStudio.FrameXml.Components.Descriptors;
using Microsoft.WowAddonStudio.FrameXml.Components.Events;
using Microsoft.WowAddonStudio.FrameXml.Components.Events.ScriptManager;
using Microsoft.WowAddonStudio.FrameXml.Components.Tests.Mocks;
using Microsoft.WowAddonStudio.FrameXml.Serialization;
using Microsoft.WowAddonStudio.Tests.Common.Mocks;

using NUnit.Framework;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Tests.FrameXmlEvents
{
	[TestFixture]
	public class FrameXmlEventDescriptorTest
	{
		#region SetUp

		[SetUp]
		public void SetUp()
		{

		}

		#endregion

		#region TearDown

		[TearDown]
		public void TearDown()
		{

		}

		#endregion

		#region Tests

		[Test(Description = "")]
		public void Test_Init()
		{
			var eventDescriptor = new FrameXmlEventDescriptor(typeof(string), EventName.OnAnimFinished);

			Assert.AreEqual("Events", eventDescriptor.Category);
			Assert.AreEqual(typeof(string), eventDescriptor.ComponentType);
			Assert.AreEqual(typeof(FrameXmlEventHandler), eventDescriptor.EventType);
			Assert.IsTrue(eventDescriptor.IsMulticast);
			Assert.AreEqual(EventName.OnAnimFinished, eventDescriptor.EventName);
		}

		[Test(Description = "")]
		public void Test_Operations()
		{
			var serviceLocator = new SimpleTestServiceLocator();
			serviceLocator.AddService(typeof(IFrameXmlScriptManager), new MockFrameXmlScriptManager());

			var eventDescriptor = new FrameXmlEventDescriptor(typeof(string), EventName.OnAnimFinished);

			var control = new ButtonControl { Site = serviceLocator };

			eventDescriptor.AddEventHandler(control, new FrameXmlEventPropertyDescriptor.GetScript(() => "print(10);"));

			Assert.IsNotNull(control.ScriptContainer);
			Assert.AreEqual(1, control.ScriptContainer.Count);
			Assert.AreEqual(EventName.OnAnimFinished, control.ScriptContainer[0].EventName);
			Assert.AreEqual("print(10);", control.ScriptContainer[0].ScriptBlock);
			eventDescriptor.RemoveEventHandler(control, new FrameXmlEventPropertyDescriptor.GetScript(() => "print(10);"));
			Assert.IsNotNull(control.ScriptContainer);
			Assert.AreEqual(0, control.ScriptContainer.Count);
		}

		#endregion
	}
}